#include "person.h"
#include <cstdlib>
#include <ctime>
#include <algorithm>



Person::Person(const Person &ob)
{
    this->mPlayerNum = ob.mPlayerNum;
    this->mPlayerName = ob.mPlayerName;
    this->mAverageScore = ob.mAverageScore;
}

int Person::getPlayerNum(void) const
{
    return this->mPlayerNum;
}

string Person::getPlayerName(void) const
{
    return this->mPlayerName;
}

float Person::getAverageScore(void) const
{
    return this->mAverageScore;
}

void Person::setPlayerNum(int num)
{
    this->mPlayerNum = num;
    return;
}

void Person::setPlayerName(string name)
{
    this->mPlayerName = name;
    return;
}

void Person::setAverageScore(float scr)
{
    this->mAverageScore = scr;
    return;
}

void showPlayer(deque<Person *> &listPlayer)
{
    deque<Person *>::iterator it = listPlayer.begin();

    for (; it != listPlayer.end(); it++)
    {
        cout << "num: " << (*it)->getPlayerNum() << " ";
        cout << "name: " << (*it)->getPlayerName() << " ";
        cout << "score: " << (*it)->getAverageScore() << endl;
    }

    return;
}

void initPalyerName(deque<Person *> &listPlayer)
{
    int i = 0;
    int j = 0;
    Person *tmp = NULL;
    string tmp_name;
    char name[32] = "";

    /* 初始化选手名字 */
    for (i = 0; i < 24; i++)
    {

        for (j = 0; j < 5; j++)
        {
            if (j == 0)
            {
                name[j] = 'A' + rand() % 26;
                continue;
            } /* end of if (j == 0) */
            name[j] = 'a' + rand() % 26;

        } /* end of for (j = 0; j < 5; j++) */

        tmp = new Person;
        tmp->setPlayerName(name);
    #ifdef DEBUG
        cout << tmp->getPlayerName() << endl;
    #endif
        listPlayer.push_back(tmp);


    } /* end of for (i = 0; i < 24; i++) */

    return;
}

void shufflePlayer(deque<Person *> &listPlayer)
{
    int i = 0;
    deque<Person *>::iterator it;

    /* 打乱顺序 */
    random_shuffle(listPlayer.begin(), listPlayer.end());

    it = listPlayer.begin();
    for (i = 101; i <= 124; i++, it++)
    {
        (*it)->setPlayerNum(i);
    } /* end of for (i = 101; i <= 124; i++) */



    return;
}


void setGroup(multimap<int, Person *> &mapPlayer, deque<Person *> &listPlayer, int n)
{
    int i = 0;
    int j = 0;

    random_shuffle(listPlayer.begin(), listPlayer.end());
    deque<Person *>::iterator it = listPlayer.begin();
    for (i = 1; i < n + 1; i++)
    {
        for (j = 0; j < 6; j++)
        {
            mapPlayer.insert({i, *it});
            it++;
        } /* end of for (j = 0; j < 6; j++) */


    } /* end of for (i = 1; i < 5; i++) */


    return;
}

void showMapPlayer(multimap<int, Person *> &mapPlayer)
{

    multimap<int, Person *>::const_iterator it1 = mapPlayer.begin();
    
    for (; it1 != mapPlayer.end(); it1++)
    {
        cout << "队伍: " << (*it1).first << " ";
        cout << "选手: " << (*it1).second->getPlayerNum() << " ";
        cout << (*it1).second->getPlayerName() << " ";
        cout << (*it1).second->getAverageScore() << endl;
    } /* end of for (; it1 != mapPlayer.end(); it1++) */

    return;
}

void traverseScore(list<int> &score)
{
    list<int>::iterator it = score.begin();

    for (; it != score.end(); it++)
    {
        cout << *it << " ";
    } /* end of for (; it != score.end(); it++) */

    cout << endl;

    return;
}

void markPlayer(multimap<int, Person *> &mapPlayer)
{
    list<int> score;
    int i = 0;
    float tmp = 0.0f;

    multimap<int, Person *>::const_iterator it = mapPlayer.begin();

    for (; it != mapPlayer.end(); it++)
    {
        for (i = 0; i < 10; i++)
        {
            score.push_back(rand() % 10 + 1);

        } /* end of for (i = 0; i < 10; i++) */
    #ifdef DEBUG
        traverseScore(score);
    #endif

        /* 去掉最高分和最低分 */
        score.sort();
        score.erase(score.begin());
        score.reverse();
        score.erase(score.begin());

        /* 求平均分 */
        tmp = accumulate(score.begin(), score.end(), 0) / 10.0;
        (*it).second->setAverageScore(tmp);


    #ifdef DEBUG
        traverseScore(score);
    #endif 

        score.clear();


    } /* end of for (; it != mapPlayer.end(); it++) */





    return;
}

void eraseList(deque<Person *> &listPlayer, Person *ptr)
{
    deque<Person *>::iterator it = listPlayer.begin();

    for (; it != listPlayer.end(); it++)
    {
        if (*it == ptr)
        {
            listPlayer.erase(it);
            break;
        } /* end of if (*it == ptr) */ 
    } /* end of for (; it != listPlayer.end(); it++) */
    //delete ptr;

    return;
}

void weedOut(multimap<int, Person *> &mapPlayer, deque<Person *> &listPlayer, int n)
{
    int i = 0;
    multimap<int, Person *>::const_iterator it;
    float minScore = 0.0f;
    int cnt = 0;
    int j = 0;




    for (j = 0; j < 3; j++)
    {
        for (i = 1; i <= n; i++)
        {
            it = mapPlayer.begin();
            cnt = 0;
            for (; it != mapPlayer.end(); it++)
            {
                if (i == (*it).first)
                {
                    if (cnt == 0)
                    {
                        minScore = (*it).second->getAverageScore();
                        cnt++;
                    }
                    else 
                    {
                        if (minScore > (*it).second->getAverageScore())
                        {
                            minScore = (*it).second->getAverageScore();
                        }
                    }
                }

            } /* end of for (; it != mapPlayer.end(); it++) */

            it = mapPlayer.begin();

            /* 寻找 */
            for (; it != mapPlayer.end(); it++)
            {   
                if (i == (*it).first && minScore == (*it).second->getAverageScore())
                {
                    break;
                }
            }

            /* 删除该选手 */
            eraseList(listPlayer, (*it).second);
            delete (*it).second;
            mapPlayer.erase(it);

        } /* end of for (i = 1; i <= 4; i++) */

    } /* end of for (j = 0; j < 3; j++) */



    return;
}